//
//  RingApplyViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/18.
//  Copyright © 2021 王洋. All rights reserved.
//
#import "CommonSubscribeRingModel.h"

#import "RingApplyViewController.h"
#import "SubscribeSegViewController.h"

@interface RingApplyViewController ()

@property (weak, nonatomic) IBOutlet UILabel *storeNameLab;

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
//@property (weak, nonatomic) IBOutlet UITextField *inviteCodeTF;
@property (weak, nonatomic) IBOutlet UITextField *ringSize_man_TF;
@property (weak, nonatomic) IBOutlet UITextField *ringSize_womman_TF;


@property (weak, nonatomic) IBOutlet UIButton *handleBtn;

@property (nonatomic, strong) CommonSubscribeRingModel *handleModel;

@end

@implementation RingApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self prepareData];
    [self initView];
    
}

- (void)initView {
    self.title = @"预约对戒";
    self.view.backgroundColor = UIColorHex(0xffffff);
    
    self.storeNameLab.text = self.seller.sellerName;
    
    [RACObserve(self.nameTF, text) subscribeNext:^(id x) {
        self.handleModel.name = self.nameTF.text;
    }];
    
    self.nameTF.text = [TTUserModel sharedObject].nickname;
    
    [RACObserve(self.phoneTF, text) subscribeNext:^(id x) {
        self.handleModel.phone = self.phoneTF.text;
    }];
    
    self.phoneTF.text = [TTUserModel sharedObject].username;
    
//    [RACObserve(self.inviteCodeTF, text) subscribeNext:^(id x) {
//        self.handleModel.recommendCode = self.inviteCodeTF.text;
//    }];
    
    [RACObserve(self.ringSize_man_TF, text) subscribeNext:^(id x) {
        self.handleModel.manSize = self.ringSize_man_TF.text;
    }];
    
    [RACObserve(self.ringSize_womman_TF, text) subscribeNext:^(id x) {
        self.handleModel.girlSize = self.ringSize_womman_TF.text;
    }];

    [[RACSignal merge:@[RACObserve(self.nameTF, text),
                        RACObserve(self.phoneTF, text),
                        RACObserve(self.ringSize_man_TF, text),
                        RACObserve(self.ringSize_womman_TF, text)
    ]] subscribeNext:^(NSString* text){

        if (self.nameTF.text.length &&
            self.phoneTF.text.length &&
            self.ringSize_man_TF.text.length &&
            self.ringSize_womman_TF.text.length ) {

            self.handleBtn.enabled = YES;
        } else {
            self.handleBtn.enabled = NO;
        }

    }];
    
}

- (void)prepareData {
    
    self.handleModel.sellerId = self.seller.sellerId;
    self.handleModel.sellerName = self.seller.sellerName;
    self.handleModel.userId = [TTUserModel sharedObject].userId;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];        
    });
}

- (IBAction)handleAction:(id)sender {
    
    [self.view endEditing:YES];
    
    if ([Utilities checkPhoneTel:self.phoneTF.text]) {
        [self request_handleApply];
    } else {
        [Utilities showToast:@"请输入正确的手机号" FColor:UIColor.whiteColor BColor:UIColorHex(0x000000) BLayerColor:UIColor.clearColor];
    }
}

- (CommonSubscribeRingModel *)handleModel {
    if (!_handleModel) {
        _handleModel = [[CommonSubscribeRingModel alloc] init];
    }
    return _handleModel;
}

//请求提交信息
- (void)request_handleApply {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ApplyRing];
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    dic = [self.handleModel modelToJSONObject];
    [SVProgressHUD showWithStatus:@"正在提交"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                [TT_Alert alert:@"您的对戒预约领取已提交成功，到期系统提醒您领取 请关注app内消息通知" title:@"提交成功" doneTitle:@"我知道了" doneAction:^{
                    SubscribeSegViewController *vc = [[SubscribeSegViewController alloc] init];
                    vc.selectIndex = 1;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }];
                
            } else {
                [Utilities showToast:[data valueForKey:@"message"] FColor:UIColor.whiteColor BColor:UIColorHex(0x000000) BLayerColor:UIColor.clearColor];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
